#include "TButton.h"
#include "TScale9.h"
#include "../DevInclude.h"


TButton::TButton()
:m_pNormal(NULL)
,m_pDown(NULL)
,m_pDisable(NULL)
 
{
    
    m_pNormalLabel=NULL;
    m_pDownLabel=NULL;
    m_pDisableLabel=NULL;
    m_bEnable=true;
    m_pListener=NULL;
    m_pfnSelector=NULL;
    m_pLabel=NULL;
}

TButton::~TButton(){
    
    
}

void TButton::onEnter(){
    TPanelItem::onEnter();
 
}


void TButton::setVisible(bool visible){
    if(isVisible()==visible){
        return;
    }
    
    TPanelItem::setVisible(visible); 
    unselected();
     
}


void TButton::setEnable(bool value){ 
    
    if(m_bEnable!=value){
        m_bEnable=value;
        if(m_bEnable){
            unselected();
        }else{
            clear();
            m_pDisable->setVisible(true);
            if(m_pDisableLabel)m_pDisableLabel->setVisible(true);
        }
    }
}

void TButton::clear(){
    setOpacity(255);
    if(m_pNormal){
        m_pNormal->setOpacity(255);
       m_pNormal->setVisible(false); 
    }
    if(m_pDown){
        m_pDown->setOpacity(255);
        m_pDown->setVisible(false);
    }
    if(m_pDisable){
        m_pDisable->setOpacity(255);
        m_pDisable->setVisible(false);
    }
    
    
    if(m_pNormalLabel)
    {
        m_pNormalLabel->setOpacity(255);
        m_pNormalLabel->setVisible(false);
    }
    if(m_pDownLabel)
    {
        m_pDownLabel->setOpacity(255);
        m_pDownLabel->setVisible(false);
    }
    
    if(m_pDisableLabel)
    {
        m_pDisableLabel->setOpacity(255);
        m_pDisableLabel->setVisible(false);
    }
}
void TButton::selected(){ 
    if(!m_bEnable){
        return;
    }
    clear();
    m_pDown->setVisible(true);
    if(m_pDownLabel)m_pDownLabel->setVisible(true);
    if (m_pListener && m_pfnSelector)
    {
        (m_pListener->*m_pfnSelector)(this);
    }
}


const char* TButton::getLabel(){
    if(m_pLabel==NULL){
        return "";
    }
    
    return m_pLabel;
}

void TButton::unselected(){
    if(!m_bEnable){ 
        return;
    }
    clear();
    if(m_pNormal) m_pNormal->setVisible(true);
    if(m_pNormalLabel)m_pNormalLabel->setVisible(true);
    
}
TButton* TButton::create(xml_node<> * pItem){
    TButton*  ret=TButton::create();
    ret->initWidthConf(pItem);
    return ret;
}

void TButton::setLabel(const char* path){
    
    m_pLabel=path;
    if(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(path)){
        if(m_pNormalLabel==NULL){
            m_pNormalLabel=CCSprite::createWithSpriteFrameName(path);
            addChild(m_pNormalLabel);
        }else{
             m_pNormalLabel->setDisplayFrame( CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(path)); 
        }
        
        m_pNormalLabel->setPosition(CCPointMake(getContentSize().width/2, -getContentSize().height/2));
        
        
        if(m_pDownLabel==NULL){
            m_pDownLabel=CCSprite::createWithSpriteFrameName(path);
            addChild(m_pDownLabel);
        }else{
            m_pDownLabel->setDisplayFrame( CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(path));
        }
        
        m_pDownLabel->setPosition(CCPointMake(getContentSize().width/2, -getContentSize().height/2));
    }
    
}

void TButton::initWidthConf(rapidxml::xml_node<> * pItem){ 
    int width=0,height=0,isScaleEffect;
    string normalPath, downPath,disablePath;
    string normalLabelPath, downLabelPath,disableLabelPath;
    
    readAttrString(pItem, "normalLabel", normalLabelPath);
    readAttrString(pItem, "downLabel", downLabelPath);
    readAttrString(pItem, "disableLabel", disableLabelPath);
    
     
    readAttrString(pItem, "normalImage", normalPath);
    readAttrString(pItem, "downImage", downPath);
    readAttrString(pItem, "disableImage", disablePath);
    
    readAttrString(pItem, "linkPanel", linkPanel);
    isScaleEffect=readAttrInt(pItem, "scaleEffect");
    
    
    CCSize size=readContainSize(pItem,getParent());
    if(size.width !=-1){
        width=size.width;
    }
    
    if(size.height!=-1){
        height=size.height;
    }
    
    if(disablePath.size()==0){
        disablePath=downPath;
    }
    
    if(disableLabelPath.size()==0){
        disableLabelPath=downLabelPath;
    }
    
   
    if(PACK_ASSETS_PLIST)
    {
        if(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(normalLabelPath.c_str())){
            m_pNormalLabel=CCSprite::createWithSpriteFrameName(normalLabelPath.c_str());
        }
        
        
        if(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(downLabelPath.c_str())){
            m_pDownLabel=CCSprite::createWithSpriteFrameName(downLabelPath.c_str());
        }
        if(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(disableLabelPath.c_str())){
            m_pDisableLabel=CCSprite::createWithSpriteFrameName(disableLabelPath.c_str());
        }
        
    }else{
        m_pNormalLabel=CCSprite::create(normalLabelPath.c_str()); 
        m_pDownLabel=CCSprite::create(downLabelPath.c_str());
        m_pDisableLabel=CCSprite::create(disableLabelPath.c_str());
    }

    
    
 
    
    if(width==0 && height==0){
        
        
        if(PACK_ASSETS_PLIST){
            
            m_pNormal=CCSprite::createWithSpriteFrameName(normalPath.c_str());
            m_pDown=CCSprite::createWithSpriteFrameName(downPath.c_str());
            m_pDisable=CCSprite::createWithSpriteFrameName(disablePath.c_str());
        }else{ 
            m_pNormal=CCSprite::create(normalPath.c_str());
            m_pDown=CCSprite::create(downPath.c_str());
            m_pDisable=CCSprite::create(disablePath.c_str());
        }
        
        if (m_pNormal) { 
            m_pNormal->setPositionX(m_pNormal->getContentSize().width/2);
            m_pNormal->setPositionY(-m_pNormal->getContentSize().height/2);
            
            setContentSize(m_pNormal->getContentSize());
        }
        
        
        if (m_pDown) {
            if (isScaleEffect) {
                m_pDown->setScaleX(1.2f);
                m_pDown->setScaleY(1.2f);
            }
            
            m_pDown->setPositionX(m_pDown->getContentSize().width/2);
            m_pDown->setPositionY(-m_pDown->getContentSize().height/2);
        }
        
        if (m_pDisable) {
            m_pDisable->setPositionX(getContentSize().width/2 );
            m_pDisable->setPositionY(-getContentSize().height/2);
        }
        
    }else{
        m_pNormal=TScale9::create(normalPath.c_str());
        m_pDown=TScale9::create(downPath.c_str());
        m_pDisable=TScale9::create(disablePath.c_str());
        int nowWidth=0,nowHeight=0;
        if(m_pNormal){
            
            nowWidth=m_pNormal->getContentSize().width;
            nowHeight=m_pNormal->getContentSize().height;
            if(width!=0)  nowWidth=width;
            if(height!=0)  nowHeight=height;
            ((TScale9*)m_pNormal)->setPreferredSize(CCSizeMake(nowWidth, nowHeight));
            
            setContentSize(m_pNormal->getContentSize());
            
        }
        
        
        if(m_pDown){ 
            if (isScaleEffect) {
                m_pDown->setScaleX(1.2f);
                m_pDown->setScaleY(1.2f);
            }
            nowWidth=m_pDown->getContentSize().width;
            nowHeight=m_pDown->getContentSize().height;
            if(width!=0)  nowWidth=width;
            if(height!=0)  nowHeight=height;
            ((TScale9*)m_pDown)->setPreferredSize(CCSizeMake(nowWidth, nowHeight));
        }
        
        
        
        if(m_pDisable){
            nowWidth=m_pDisable->getContentSize().width;
            nowHeight=m_pDisable->getContentSize().height;
            if(width!=0)  nowWidth=width;
            if(height!=0)  nowHeight=height;
            ((TScale9*)m_pDisable)->setPreferredSize(CCSizeMake(nowWidth, nowHeight));
        
        }
        
        
        
        
    }
    
    
   if(m_pNormal)  addChild(m_pNormal);
   if(m_pDown) addChild(m_pDown);
   if(m_pDisable) addChild(m_pDisable);
    
    
 
     
    
    
    if(m_pNormalLabel) {
       addChild(m_pNormalLabel);
        m_pNormalLabel->setPosition(CCPointMake(getContentSize().width/2, -getContentSize().height/2));
    }
    if(m_pDownLabel){
        addChild(m_pDownLabel);
        m_pDownLabel->setPosition(CCPointMake(getContentSize().width/2, -getContentSize().height/2));
    }
    if(m_pDisableLabel){
        addChild(m_pDisableLabel);
        m_pDisableLabel->setPosition(CCPointMake(getContentSize().width/2, -getContentSize().height/2));
    }
    
    
    
    unselected();
}

void TButton::setTarget(CCObject*   obj,SEL_MenuHandler selector){
    
    m_pListener=obj;
    m_pfnSelector=selector;
}


